<?php

namespace app\api\model;

use app\common\model\BaseModel;

class CourseComment extends BaseModel
{
    protected $autoWriteTimestamp = 'int';
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';


    public function tag()
    {
        return $this->belongsToMany(Tag::class, 'course_comment_tag', 'tag_id', 'course_comment_id');
    }

    public function reply()
    {
        return $this->hasMany(CourseReply::class, 'course_comment_id', 'course_comment_id');
    }

    public function user()
    {
        return $this->hasOne(User::class, 'user_id', 'user_id')->field(['user_id', 'nickname', 'avatar', 'mobile']);
    }

    /**
     * 课程评论
     */
    public static function getComments($teacherCourseId, $size = 20)
    {
        $comments = self::with(['user', 'tag', 'reply'])->where(static::whereOnline())->where('teacher_course_id', $teacherCourseId)->order('createtime desc')->paginate($size);
        $res = self::dealData($comments->items());
        if ($res) {
            $res['total'] = $comments->total();
        }
        return $res;
    }

    /**
     * 获取最新2条
     */
    public static function getCommentsByLimit($teacherCourseId, $limit = 2)
    {
        $comments = self::with(['user', 'tag', 'reply'])->where(static::whereOnline())->where('teacher_course_id', $teacherCourseId)->order('createtime desc')->limit($limit)->select();
        return self::dealData($comments);
    }

    /**
     * 处理数据
     */
    public static function dealData($comments)
    {
        if (!$comments) {
            return [];
        }
        array_walk($comments, function (&$val) {
            if ($val['user']) {
                $val['user']['mobile'] = substr_replace($val['user']['mobile'], '****', 3, 4);
            }
        });
        $count = count($comments);
        //效果满意度评分
        $resultScore = array_sum(array_column($comments, 'result_score')) / $count;
        $resultLike = $resultScore / 5 * 100;
        //服务满意度平分
        $serviceScore = array_sum(array_column($comments, 'service_score')) / $count;
        $serviceLike = $serviceScore / 5 * 100;
        //综合评分
        $comScore = ceil(array_sum(array_column($comments, 'com_score')) / $count);
        //标签
        $tag = Tag::getInTagId(CourseCommentTag::getByCourseId(array_column($comments, 'course_comment_id')));
        return [
            'comments' => $comments,
            'com_score' => $comScore,
            'result_like' => $resultLike % 2 ? numberFormat($resultLike).'%' : numberFormat($resultLike, 0).'%',
            'service_like' => $serviceLike % 2 ? numberFormat($serviceLike).'%' : numberFormat($serviceLike, 0).'%',
            'tag' => $tag
        ];
    }

    /**
     * 添加评论
     */
    public function add(array $data)
    {
        return $this->allowField(true)->save($data);
    }

    /**
     * 综合评分
     */
    public static function comScore($teacherCoureId)
    {
        //总综合分
        $comScore = self::where('teacher_course_id', $teacherCoureId)->sum('com_score');
        //总数
        $count = self::where('teacher_course_id', $teacherCoureId)->count();
        return number_format($comScore / $count, 1);
    }

    /**
     * 课程评论总数
     */
    public static function getCommentCounts($teacherCourseId)
    {
        return self::where('teacher_course_id', $teacherCourseId)->where(static::whereOnline())->count();
    }
}
